package com.example.totolotek;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class czytajStrone {
	 static void a(String[] a){
	        for (String string : a) {
	            System.out.print(" "+string);
	        }
	            System.out.println("" );
	    }
	    public czytajStrone() throws MalformedURLException, IOException
	    {
	        //System.out.println( "Hello World!" ); 
	        URL yahoo = new URL("http://www.lotto.pl/");
	        URLConnection yc = yahoo.openConnection();
	        BufferedReader in = new BufferedReader(
	                                new InputStreamReader(
	                                yc.getInputStream()));
	        String inputLine;
	        boolean lotto=false,wyniklotto=false,lottoplus=false,minilotto=false,mm=false;
	        int licznik=0;
	        String[] liczbylotto={"","","","","",""},liczbylotto2={"","","","","",""},liczbylotto3={"","","","",""},
	                liczbylotto4={"","","","","","","","","","","","","","","","","","","",""};
	        while (( inputLine = in.readLine()) != null ) 
	        {
	            
	            if( inputLine.indexOf("Multi Multi")!=-1 ){
	                lotto=wyniklotto=lottoplus=minilotto=false;mm=true;
	            }
	            
	            if( inputLine.indexOf("Lotto")!=-1 ){
	                mm=wyniklotto=lottoplus=minilotto=false;lotto=true;
	            }
	            if( inputLine.indexOf("Lotto Plus")!=-1 ){
	                lotto=wyniklotto=mm=minilotto=false;lottoplus=true;
	            }
	            if( inputLine.indexOf("Mini Lotto")!=-1 ){
	              //  lottoplus=lotto=false;minilotto=true;;
	                lotto=wyniklotto=mm=lottoplus=false;minilotto=true;
	            }
	            if( mm )
	            {
	                d(inputLine,">>>>>lotto mm");
	                if( inputLine.indexOf("wynik_multi-multi")!=-1 )
	                    {
	                      String a=inputLine.substring(inputLine.indexOf("wynik_multi-multi")+19,inputLine.indexOf("wynik_multi-multi")+21);  
	                     System.out.println("mm "+a);
	                      liczbylotto4[licznik]=a;
	                      ++licznik;
	                      if( licznik==20 )
	                      {
	                          mm=false;
	                          licznik=0;
	                      }
	                    }
	            }
	            if( minilotto )
	            {
	                d(inputLine,">>>>>lotto minilotto");
	                if( inputLine.indexOf("wynik_mini-lotto")!=-1 )
	                    {
	                      String a=inputLine.substring(inputLine.indexOf("wynik_mini-lotto")+18,inputLine.indexOf("wynik_mini-lotto")+20);  
	                     System.out.println("minilotto "+a);
	                      liczbylotto3[licznik]=a;
	                      ++licznik;
	                      if( licznik==5 )
	                      {
	                          minilotto=false;
	                          licznik=0;
	                      }
	                    }
	            }
	            if( lottoplus )
	            {
	                d(inputLine,">>>>>lottoplus");
	                if( inputLine.indexOf("wynik_lotto")!=-1 )
	                    {
	                      String a=inputLine.substring(inputLine.indexOf("wynik_lotto")+13,inputLine.indexOf("wynik_lotto")+15);  
	                     System.out.println("lottoplus "+a);
	                      liczbylotto2[licznik]=a;
	                      ++licznik;
	                      if( licznik==6 )
	                      {
	                          lottoplus=false;
	                          licznik=0;
	                      }
	                    }
	            }
	            if( lotto ) 
	            {
	                d(inputLine,">>>>>lotto");
	                    if( inputLine.indexOf("wynik_lotto")!=-1 )
	                    {
	                      String a=inputLine.substring(inputLine.indexOf("wynik_lotto")+13,inputLine.indexOf("wynik_lotto")+15);  
	                     System.out.println("lotto "+a);
	                      liczbylotto[licznik]=a;
	                      ++licznik;
	                      if( licznik==6 )
	                      {
	                          lotto=false;
	                          licznik=0;
	                      }
	                    }
	        
	            }
	        }
	        in.close();
	        a(liczbylotto);
	        a(liczbylotto2);
	        a(liczbylotto3);
	                a(liczbylotto4);
	    }
	    static void d(String a,String inputLine){
	                if( inputLine.indexOf("wyniki z dnia")!=-1 ) 
	                {
	                    String aa=inputLine.substring(inputLine.indexOf("wyniki z dnia")+22,inputLine.indexOf("wyniki z dnia")+30);
	                    System.out.println(a+aa);
	                }
	    }
}
